package com.guodx.study.springboot.d09.framework.apilog.config;

import com.guodx.study.springboot.d09.framework.apilog.core.filter.ApiAccessFilter;
import com.guodx.study.springboot.d09.framework.apilog.core.filter.CacheRequestBodyFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ApilogAutoConfig {

    /**
     * 创建过滤器
     * @param applicationName
     * @return
     */
    @Bean
    // 允许使用access-log.enabled=false来关闭功能
    @ConditionalOnProperty(prefix = "access-log", value = "enabled", matchIfMissing = true)
    public ApiAccessFilter apiAccessFilter(@Value("${spring.application.name}") String applicationName ){
        return new ApiAccessFilter(applicationName);
    }

    /**
     * 创建 RequestBodyCacheFilter Bean，可重复读取请求内容
     */
    @Bean
    public FilterRegistrationBean<CacheRequestBodyFilter> cacheRequestBodyFilter(){
        CacheRequestBodyFilter cacheRequestBodyFilter = new CacheRequestBodyFilter();
        FilterRegistrationBean<CacheRequestBodyFilter> bean = new FilterRegistrationBean<>(cacheRequestBodyFilter);
        bean.setOrder(Integer.MIN_VALUE + 500);
        return bean;
    }


}
